home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261src.zip / GXREFCT.H < prev    next >
C/C++ Source or Header  |  1993-05-17  |  4KB  |  105 lines

  1. /* Copyright (C) 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gxrefct.h */
  20. /* Definitions for reference-counted data structures */
  21.  
  22. #ifndef gxrefct_INCLUDED
  23. #  define gxrefct_INCLUDED
  24.  
  25. /*
  26.  * A reference-counted object must include the following header:
  27.  *    rc_header rc;
  28.  */
  29. typedef struct rc_header_s {
  30.     long ref_count;
  31.     uint size;
  32. } rc_header;
  33.  
  34. /* ------ Allocate/free ------ */
  35.  
  36. #define rc_alloc_n(vp, stype, ssize, mprocs, errstat, cname, rcinit)\
  37.    {    if ( (vp = (stype *)(*(mprocs)->alloc)(1, ssize, cname)) == 0 )\
  38.        errstat;\
  39.     vp->rc.ref_count = rcinit;\
  40.     vp->rc.size = ssize;\
  41.    }
  42. #define rc_alloc_0(vp, stype, ssize, mprocs, errstat, cname)\
  43.   rc_alloc_n(vp, stype, ssize, mprocs, errstat, cname, 0)
  44. #define rc_alloc_1(vp, stype, ssize, mprocs, errstat, cname)\
  45.   rc_alloc_n(vp, stype, ssize, mprocs, errstat, cname, 1)
  46.  
  47. #define rc_alloc_struct_0(vp, stype, mprocs, errstat, cname)\
  48.   rc_alloc_0(vp, stype, sizeof(stype), mprocs, errstat, cname)
  49. #define rc_alloc_struct_1(vp, stype, mprocs, errstat, cname)\
  50.   rc_alloc_1(vp, stype, sizeof(stype), mprocs, errstat, cname)
  51. #define rc_alloc_struct_n(vp, stype, mprocs, errstat, cname, rcinit)\
  52.   rc_alloc_n(vp, stype, sizeof(stype), mprocs, errstat, cname, rcinit)
  53.  
  54. #define rc_free(vp, mprocs, cname)\
  55.   (*(mprocs)->free)((char *)vp, 1, (vp)->rc.size, cname)
  56.  
  57. /* ------ Reference counting ------ */
  58.  
  59. /* Increment a reference count. */
  60. #define rc_increment(vp)\
  61.   if ( vp != 0 ) vp->rc.ref_count++
  62.  
  63. /* Increment a reference count, allocating the structure if necessary. */
  64. #define rc_allocate(vp, stype, ssize, mprocs, errstat, cname)\
  65.   if ( vp != 0 )\
  66.     vp->rc.ref_count++;\
  67.   else\
  68.    {    rc_alloc_1(vp, stype, ssize, mprocs, errstat, cname);\
  69.    }
  70. #define rc_allocate_struct(vp, stype, mprocs, errstat, cname)\
  71.   rc_allocate(vp, stype, sizeof(stype), mprocs, errstat, cname)
  72.  
  73. /* Guarantee that a structure is not shared. */
  74. #define rc_unshare(vp, stype, mprocs, errstat, cname)\
  75.   if ( vp == 0 || vp->rc.ref_count > 1 )\
  76.    {    stype *new;\
  77.     rc_alloc_1(new, stype, vp->rc.size, mprocs, errstat, cname);\
  78.     if ( vp ) vp->rc.ref_count--;\
  79.     vp = new;\
  80.    }
  81.  
  82. /* Decrement a reference count, freeing the structure if necessary. */
  83. #define rc_decrement(vp, mprocs, cname)\
  84.   if ( vp != 0 && !--(vp->rc.ref_count) )\
  85.    {    rc_free(vp, mprocs, cname);\
  86.     vp = 0;\
  87.    }
  88.  
  89. /* Adjust a reference count either up or down. */
  90. #define rc_adjust(vp, delta, mprocs, cname)\
  91.   if ( vp != 0 && !(vp->rc.ref_count += delta) )\
  92.    {    rc_free(vp, mprocs, cname);\
  93.     vp = 0;\
  94.    }
  95.  
  96. /* Assign a pointer, adjusting reference counts. */
  97. #define rc_assign(vpto, vpfrom, mprocs, cname)\
  98.   if ( vpto != vpfrom )\
  99.    {    rc_decrement(vpto, mprocs, cname);\
  100.     vpto = vpfrom;\
  101.     rc_increment(vpto);\
  102.    }
  103.  
  104. #endif                    /* gxrefct_INCLUDED */
  105.